<?php


namespace app\service;


use think\Db;

class TaskService
{
    /**
     * @param $biz_id
     * @return array|false[]
     * @context 获取当日任务
     */
    static function todayBizTask($biz_id){
        # 获取今日任务
       a: $info = Db::table("biz_daytask")->where(array("biz_id"=>$biz_id))->where("date(create_time)='".date("Y-m-d")."'")->find();
        if(empty($info)){
           # 查询该门店是否为A类门店 并且返现6个月内的
            $bizInfo = Db::table("biz")->field("recommend_status,expair_time")->where(array("id"=>$biz_id))->find();
            if(!empty($bizInfo) and $bizInfo['recommend_status']==1 and $bizInfo['expair_time']>=date("Y-m-d")){
                # 添加今日任务
              $return = self::addTask($biz_id,date("Y-m-d"));
              if ($return['status']){
                  goto a;
              }else{
                  return array("status"=>false);
              }

            }else{
                return array("status"=>false);
            }
        }
        if(!empty($info) and $info['task_status']==2 and $info['task_type']==6){
            # 好评任务就踏马隔路，查询明日是否有任务
           $is_tomorrow = Db::table("biz_daytask")->where(array("biz_id"=>$biz_id))->where("date(create_time)='".date("Y-m-d",strtotime('+1 day'))."'")->find();
           if(empty($is_tomorrow)){
               self::addTask($biz_id,date("Y-m-d",strtotime('+1 day')));
           }
        }
        return array("status"=>true,"info"=>$info,"task_price"=>13.8);
    }

    /**
     * @param $biz_id
     * @param $type
     * @return bool
     * @context 完成当日任务
     */
    static function finishDayTask($biz_id,$type){
        # 查询是否有今日任务
        $info = Db::table("biz_daytask")->where(array("biz_id"=>$biz_id,"task_type"=>$type,"task_status"=>1))->where("date(create_time)='".date("Y-m-d")."'")->find();
        if(!empty($info)){
            $finishStatus = false;
            if($info['task_plan']+1>=$info['task_amount']){
                $_array =array("task_plan"=>$info['task_plan']+1,"task_status"=>2);
                $finishStatus = true;
            }else{
                $_array =array("task_plan"=>$info['task_plan']+1);
            }
            Db::table('biz_daytask')->where(array("id"=>$info['id']))->update($_array);
            if($info['task_status']==1 and $finishStatus){
                # 领取奖励并添加明日任务
                self::receivePrice($biz_id);
                self::addTask($biz_id,date("Y-m-d",strtotime('+1 day')));
            }
        }
        return true;
    }

    /**
     * @param $biz_id
     * @return bool
     * @context 领取任务奖励
     */
    static function receivePrice($biz_id){
        $price = 13.8;
        Db::table('biz')->where(array('id' => $biz_id))->setInc('account_balance', $price);
        addSubsidy(array("subsidy_type" => 17, "subsidy_number" => $price,'member_id'=>0, "biz_id" => $biz_id,'pid'=>0));
        return true;
    }

    /**
     * @param $biz_id
     * @param $date  添加某一天的日期
     * @return array|false[]
     * @context 添加任务
     */
    static function addTask($biz_id,$date)
    {
        $log_upgrade = Db::table("biz_log_upgrade")->field("date(create_time) create_time")->where(array("biz_id"=>$biz_id,"type"=>1))->find();
        if(!empty($log_upgrade) and $log_upgrade['create_time']>=date("Y-m-d",strtotime("-6 month",strtotime($date)))){
            # 有效期内判断添加哪个任务
            if(date("Y-m-d",strtotime('-15 days',strtotime($date))) <= $log_upgrade['create_time']){
                # 在15天内 计算相差多少天
                $diffDay = timediff($log_upgrade['create_time'],$date,false);
                $num = $diffDay['day'];
                $taskInfo = self::getsDayTask(1,$num);
            }elseif(date("Y-m-d",strtotime('-15 days',strtotime($date))) > $log_upgrade['create_time']
                and date("Y-m-d",strtotime('-1 month',strtotime($date))) <= $log_upgrade['create_time']){
                # 在15天到一个月内
                $taskInfo = self::getsDayTask(2);
            }elseif(date("Y-m-d",strtotime('-1 month',strtotime($date))) > $log_upgrade['create_time']
                and date("Y-m-d",strtotime('-2 month',strtotime($date))) <= $log_upgrade['create_time']){
                # 1个月到2个月
                $taskInfo = self::getsDayTask(3);
            }elseif(date("Y-m-d",strtotime('-2 month',strtotime($date))) > $log_upgrade['create_time']){
                # 2个月到6个月
                $taskInfo = self::getsDayTask(4);
            }
            if(!empty($taskInfo)){
                $_array =array("biz_id"=>$biz_id,"task_type"=>$taskInfo['type'],"task_plan"=>0,"task_amount"=>$taskInfo['num'],
                    "task_status"=>1,"create_time"=>date("{$date} H:i:s"),"task_title"=>$taskInfo['title']);
                $id = Db::table('biz_daytask')->insertGetId($_array);
                $_array['id'] = $id;
                return array("status"=>true,"info"=>$_array);
            }else{
                return array("status"=>false);
            }
        }else{
            return array("status"=>false);
        }
    }

    /**
     * @param $type
     * @param int $num
     * @return array
     * @context 获取要添加的任务信息
     */
    static function getsDayTask($type,$num=0){
        if($type==1){
            $info = self::taskCon1();
        }elseif($type==2){
            $num = rand(0,8);
            $info = self::taskCon2();
        }elseif($type==3){
            $num = rand(0,6);
            $info = self::taskCon3();
        }else{
            $num = rand(0,6);
            $info = self::taskCon4();
        }
        return $info[$num];
    }
    /**
     * @return array[]
     * @context 前15天任务按顺序 完成任务给13.8
     */
    static function taskCon1(){
        return array(
            array("title"=>"完成上架门店服务","num"=>1,"type"=>1),
            array("title"=>"完成门店录入员工","num"=>1,"type"=>2),
            array("title"=>"设置门店员工提成","num"=>1,"type"=>3),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成上架门店服务","num"=>1,"type"=>1),
            array("title"=>"完成上架门店服务","num"=>1,"type"=>1),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
        );
    }

    /**
     * @return array[]
     * @context 15天-1个月任务随机
     */
    static function taskCon2(){
        return array(
            array("title"=>"完成门店服务","num"=>1,"type"=>5),
            array("title"=>"获得服务好评","num"=>1,"type"=>6),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成门店服务","num"=>1,"type"=>5),
            array("title"=>"完成门店服务","num"=>1,"type"=>5),
            array("title"=>"获得服务好评","num"=>1,"type"=>6),
            array("title"=>"完成今日员工考勤","num"=>1,"type"=>4),
            array("title"=>"完成门店服务","num"=>1,"type"=>5),
        );
    }

    /**
     * @return array[]
     * @context 1-2个月任务随机
     */
    static function taskCon3(){
        return array(
            array("title"=>"完成门店服务","num"=>2,"type"=>5),
            array("title"=>"获得服务好评","num"=>1,"type"=>6),
            array("title"=>"完成门店服务","num"=>3,"type"=>5),
            array("title"=>"获得服务好评","num"=>2,"type"=>6),
            array("title"=>"完成门店服务","num"=>2,"type"=>5),
            array("title"=>"完成门店服务","num"=>1,"type"=>5),
        );
    }

    /**
     * @return array[]
     * @context 2-6个月任务随机
     */
    static function taskCon4(){
        return array(
            array("title"=>"完成门店服务","num"=>3,"type"=>5),
            array("title"=>"获得服务好评","num"=>2,"type"=>6),
            array("title"=>"完成门店服务","num"=>3,"type"=>5),
            array("title"=>"获得服务好评","num"=>1,"type"=>6),
            array("title"=>"完成门店服务","num"=>4,"type"=>5),
            array("title"=>"完成门店服务","num"=>2,"type"=>5),
        );
    }

}